package com.hundun.hddeal.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Map;

/**
 * 参数提取token
 */
public class TokenUtilT {

    /**
     * 公共密钥
     */
    public static String SECRET="FreeMaNong";


    public static String getTokenById(String token) {
        /*String UserId = JWT.decode(token).getAudience().get(0);
        if(UserId==null){
            return "无token，重新登陆";
        }
        return UserId;
    }*/
        Map<String, Claim> claimMap = verifyToken(token);
        String UserId = claimMap.get("name").asString();
        return UserId;
    }


    //解密
    public  static Map<String, Claim> verifyToken(String token){
        JWTVerifier verifier= JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt=null;
        try{
            jwt=verifier.verify(token);
        }catch (Exception e){
            throw new RuntimeException("登陆凭证已过期,请重新登陆!");
        }

        return jwt.getClaims();


    }


}